# Contributor: <xmingske@gmail.com>
# Contributor: Sascha Paunovic <azarus@posteo.net>
# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Contributor: David Heidelberg <david@ixit.cz>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=minetest
pkgver=5.9.1
pkgrel=1
pkgdesc="An infinite-world block sandbox game and a game engine"
url="https://www.minetest.net/"
arch="all"
license="LGPL-2.1-or-later AND CC-BY-SA-3.0"
pkgusers="minetest"
pkggroups="minetest"
install="$pkgname-server.pre-install"
depends="$pkgname-common"

makedepends="
	bzip2-dev
	cmake
	curl-dev
	freetype-dev
	gettext-dev
	hiredis-dev
	libjpeg-turbo-dev
	libogg-dev
	libpng-dev
	libvorbis-dev
	libxi-dev
	mesa-dev
	openal-soft-dev
	openssl-dev>3
	samurai
	sdl2-dev
	sqlite-dev
	zstd-dev
	"
subpackages="$pkgname-doc
	$pkgname-lang
	$pkgname-common::noarch
	$pkgname-server
	$pkgname-server-openrc:openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/minetest/minetest/archive/$pkgver.tar.gz
	minetest-server.confd
	minetest-server.initd
	disable-TestLua.patch
	"

# These arch has no luajit package available
case "$CARCH" in
	ppc64le|riscv64|loongarch64)
		_luajit_supported=0
		;;
	*)
		_luajit_supported=1
		makedepends="$makedepends luajit-dev"
;;
esac

build() {
	cmake -G Ninja -B build_client \
		-DCMAKE_BUILD_TYPE=Release \
		-DENABLE_LTO=1 \
		-DCUSTOM_BINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCUSTOM_DOCDIR="/usr/share/doc/$pkgname" \
		-DCUSTOM_SHAREDIR="/usr/share/$pkgname" \
		-DBUILD_CLIENT=1 \
		-DBUILD_SERVER=0 \
		-DENABLE_REDIS=0 \
		-DENABLE_CURL=1 \
		-DENABLE_LUAJIT="$_luajit_supported" \
		-DENABLE_UPDATE_CHECKER=0 \
		-DRUN_IN_PLACE=0 \
		-DUSE_SDL2=1
	cmake --build build_client

	cmake -G Ninja -B build_server \
		-DCMAKE_BUILD_TYPE=Release \
		-DENABLE_LTO=1 \
		-DCUSTOM_BINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCUSTOM_DOCDIR="/usr/share/doc/minetest" \
		-DCUSTOM_SHAREDIR="/usr/share/minetest" \
		-DBUILD_CLIENT=0 \
		-DBUILD_SERVER=1 \
		-DENABLE_REDIS=1 \
		-DENABLE_CURL=1 \
		-DENABLE_LUAJIT="$_luajit_supported" \
		-DENABLE_UPDATE_CHECKER=0 \
		-DRUN_IN_PLACE=0
	cmake --build build_server
}

check() {
	bin/minetest --run-unittests
	bin/minetestserver --run-unittests
}

package() {
	DESTDIR="$pkgdir" cmake --install build_client
}

common() {
	pkgdesc="Minetest files used by both client & server"
	depends=""

	install -o minetest -g minetest -d "$subpkgdir"/var/lib/minetest
	amove \
		usr/share/minetest/builtin
}

server() {
	pkgdesc="Minetest server"
	depends="$pkgname-common"

	cd "$builddir"
	install -Dm644 minetest.conf.example \
		"$subpkgdir"/etc/minetest/minetest.conf
	install -Dm755 bin/minetestserver \
		-t "$subpkgdir"/usr/bin
}

openrc() {
	pkgdesc="Minetest server (OpenRC init scripts)"
	install_if="$pkgname-server=$pkgver-r$pkgrel openrc"

	install -Dm755 "$srcdir"/minetest-server.initd \
		"$subpkgdir"/etc/init.d/minetest-server
	install -Dm644 "$srcdir"/minetest-server.confd \
		"$subpkgdir"/etc/conf.d/minetest-server
}

sha512sums="
e4a03cd7f0a745f0571dd3e9701ddf48e079c59dc0bf0c59ecf756151d03a82422213e7198f359e93852b83111430e2db8c2f5f92020b3244f64e197fd51c93c  minetest-5.9.1.tar.gz
7bca17dc0bd59db9c07c160677498aa09297026104e922bf93b7629c274d0aebd226af60c5e82f49ffe3cc5c1e4448e3798b6c2a44144fc6eca4d3aaed3384bb  minetest-server.confd
1e2d64357b46d7d8d5f9e8e6857b1d75aed57eef4674e78bc36ea560bd0af4bc88122c567e8fbd23a992e244931299bfe39fe6a6625c3afac48c28053a73e839  minetest-server.initd
4783a274d65f0ba5c4c4507d47e2f4a43e614b917fbbf64c6dff9837ceb8a6edd8f2313c638c4c0041333a409ba1a230f93c974402e22bd1d781be3ebde59dee  disable-TestLua.patch
"
